package com.ruoyi.nongjt.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.nongjt.domain.Region;
import com.ruoyi.nongjt.mapper.RegionMapper;
import com.ruoyi.nongjt.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author yunxi
 */

@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    private RegionMapper regionMapper;

    @Override
    public List<Region> selectChildRegions(Region region) {
        // 处理根节点情况
        if (region.getParentId() == null) {
            region.setParentId(0);
        }
        return regionMapper.selectRegionList(region);
    }

    @Override
    public Region selectRegionById(Integer id) {
        return regionMapper.selectById(id);
    }

    @Transactional
    @Override
    public int insertRegion(Region region) {
        // 设置创建人信息
        String username = SecurityUtils.getUsername();
        region.setCreateBy(username);
        region.setCreateTime(new Date());
        region.setUpdateBy(username);
        region.setUpdateTime(new Date());

        // 默认为正常状态
        if (region.getStatus() == null) {
            region.setStatus(1);
        }

        // 新增区域
        int rows = regionMapper.insert(region);

        // 如果有父节点，更新父节点的has_children状态
        if (region.getParentId() != null && region.getParentId() != 0) {
            regionMapper.updateParentHasChildren(region.getParentId());
        }

        return rows;
    }

    @Transactional
    @Override
    public int updateRegion(Region region) {
        // 设置更新人信息
        region.setUpdateBy(SecurityUtils.getUsername());
        region.setUpdateTime(new Date());

        // 查询原始区域信息
        Region oldRegion = regionMapper.selectById(region.getId());
        int rows = regionMapper.update(region);

        // 如果父节点发生变化，更新新旧父节点的has_children状态
        if (oldRegion != null && !oldRegion.getParentId().equals(region.getParentId())) {
            regionMapper.updateParentHasChildren(oldRegion.getParentId());
            regionMapper.updateParentHasChildren(region.getParentId());
        }

        return rows;
    }

    @Transactional
    @Override
    public int deleteRegionById(Integer id) {
        int totalRows = 0;

        // 1. 查询所有子区域
        Region query = new Region();
        query.setParentId(id);
        List<Region> children = regionMapper.selectRegionList(query);

        // 2. 递归删除所有子区域
        for (Region child : children) {
            totalRows += deleteRegionById(child.getId());
        }

        // 3. 删除当前区域
        int rows = regionMapper.deleteById(id);
        totalRows += rows;

        // 4. 更新父节点的has_children状态
        Region region = regionMapper.selectById(id);
        if (region != null && region.getParentId() != 0) {
            regionMapper.updateParentHasChildren(region.getParentId());
        }

        return totalRows;
    }
}